package com.gavindon.android.base

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders

/**
 * description:
 * Created by liNan on  2021/5/6 11:14
 */
abstract class BaseFragment : Fragment() {


    @get:LayoutRes
    abstract val layoutId: Int

    abstract fun onInit(view: View, savedInstanceState: Bundle?)


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.i("lifecycle", "onCreateView-${this.javaClass.simpleName}")
        return inflater.inflate(layoutId, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.i("lifecycle", "onViewCreated-${this.javaClass.simpleName}")
        onInit(view, savedInstanceState)
    }


    override fun onPause() {
        super.onPause()
        val im = this.context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        im.hideSoftInputFromWindow(this.view?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
        Log.i("lifecycle", "onPause-${this.javaClass.simpleName}")
    }

    /**
     * 在java用泛型时不能够直接地使用类型。
     * 而在kotlin中使用inline配合reified可以使用泛型类型
     */
    inline fun <reified V : BaseViewModel> getViewModel(): V {
        return ViewModelProviders.of(this).get(V::class.java)
    }


}